import { Body, Controller, Delete, Get, Param, ParseIntPipe, Patch, Post, Query } from "@nestjs/common";
import { ApiOperation, ApiTags } from "@nestjs/swagger";

import { SkipJwtAuth } from "@/core/auth/decorator/skip-auth.decorator";
import { ApiBaseResponse } from "@/core/decorators/api-base-response.decorator";
import { ApiPageResponse } from "@/core/decorators/api-page-response.decorator";
import AjaxResult from "@/core/utils/AjaxResult";

import { CreateHotlistDto } from "./dto/create-hotlist.dto";
import { QueryHotlistDto } from "./dto/query-hotlist.dto";
import { UpdateHotlistDto } from "./dto/update-hotlist.dto";
import { HotlistEntity } from "./entities/hotlist.entity";
import { HotlistService } from "./hotlist.service";

@ApiTags("热榜头条")
@Controller("hotlist")
export class HotlistController {
    constructor(private readonly hotlistService: HotlistService) {}

    @ApiOperation({ summary: "头条添加" })
    @ApiBaseResponse(HotlistEntity)
    @Post("/add")
    async create(@Body() createHotlistDto: CreateHotlistDto) {
        return AjaxResult.success(await this.hotlistService.create(createHotlistDto));
    }

    @SkipJwtAuth()
    @ApiOperation({ summary: "头条列表" })
    @ApiBaseResponse({ type: "array", items: HotlistEntity })
    @Get("/list")
    async list() {
        return AjaxResult.success(await this.hotlistService.findAll());
    }

    @SkipJwtAuth()
    @ApiOperation({ summary: "热搜分页" })
    @ApiPageResponse(HotlistEntity)
    @Get("/page")
    async page(@Query() query: QueryHotlistDto) {
        return AjaxResult.success(await this.hotlistService.findPage(query));
    }

    @ApiOperation({ summary: "头条详情" })
    @ApiBaseResponse(HotlistEntity)
    @Get("/detail/:id")
    async findOne(@Param("id", ParseIntPipe) id: number) {
        return AjaxResult.success(await this.hotlistService.findOne(+id));
    }

    @ApiOperation({ summary: "头条更新" })
    @ApiBaseResponse(HotlistEntity)
    @Patch("/edit/:id")
    async edit(@Param("id", ParseIntPipe) id: number, @Body() updateHotlistDto: UpdateHotlistDto) {
        return AjaxResult.success(await this.hotlistService.update(+id, updateHotlistDto));
    }

    @ApiOperation({ summary: "头条删除" })
    @Delete("/delete/:id")
    async remove(@Param("id", ParseIntPipe) id: number) {
        return AjaxResult.success(await this.hotlistService.remove(+id));
    }
}
